package com.wx.disparent.mallparent.service;

import com.wx.disparent.mallparent.commons.inface.IBaseService;
import com.wx.disparent.mallparent.dao.MerchantMapper;
import com.wx.disparent.mallparent.entity.MerchantEntity;
import com.wx.disparent.mallparent.entity.ParaEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description 商家服务层
 * @author 刘杰龙
 * @version 1.0
 * @date 2019/12/10 9:24
 */
@Service
public class MerchantService implements IBaseService<MerchantEntity> {
    @Autowired
    private MerchantMapper mapper;
    @Autowired
    private ProductService productService;
    /**查询所有商家信息*/
    @Override
    public List<MerchantEntity> findAll(Map<String, Object> params) {
        List<MerchantEntity> merchantEntities = mapper.findAll(params);
        return merchantEntities;
    }

    /**查询单个商家信息*/
    @Override
    public MerchantEntity findById(int t) {
        MerchantEntity merchantEntity = mapper.findById(t);
        return merchantEntity;
    }

    /**查询单个商家信息(by phone)*/
    public MerchantEntity findByPhone(String s) {
        MerchantEntity merchantEntity = mapper.findByPhone(s);
        return merchantEntity;
    }

    /**查询单个商家信息(by name)*/
    public MerchantEntity findByName(String s) {
        MerchantEntity merchantEntity = mapper.findByName(s);
        return merchantEntity;
    }

    public List<ParaEntity> findAllPara(Map<String, Object> params){
        List<ParaEntity> allPara = mapper.findAllPara(params);
        return allPara;
    }

    /**添加新商家*/
    @Override
    public int insert(MerchantEntity merchantEntity) {
        int result = mapper.insert(merchantEntity);
        return result;
    }

    /**修改商家信息*/
    @Override
    public int update(MerchantEntity merchantEntity) {
        int result = mapper.update(merchantEntity);
        return result;
    }

    @Override
    public int deleteById(int t) {
        return 0;
    }

    /**修改商家状态：0.可用  2.禁用*/
    public int changeStatus(int t,String status) {
        Map<String,Object> params = new HashMap();
        if (status.equals("available")){
            params.put("status","0");
        }else if (status.equals("disabled")){
            params.put("status","1");
            productService.changeByMerchant(t,"sold");
        }
        params.put("id",t);

        int result = mapper.changeStatus(params);
        return result;
    }
}
